package com.atwisdom.star.dcs.Thread;

import cn.hutool.core.date.DateTime;
import com.atwisdom.star.dcs.dao.RealTimeInfoTempDao;
import com.atwisdom.star.dcs.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ClearTempDataTread implements Runnable  {

    @Autowired
    private RealTimeInfoTempDao realTimeInfoTempDao;

    @Value("${das.cleanDays}")
    private long cleanDays;

    @Value("${das.cleanHour}")
    private long cleanHours;
    @Override
    public void run() {
        DateTime current = new DateTime();
        while (true) {
            if(current.hour(true) == cleanHours){
                System.out.println("开始清理临时表数据，我每天只跑一次，尽量不会影响你们工作");
                try {
                    this.clearUp();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                System.out.println("今天的清理结束了，明天"+cleanHours+"点重新开始");
                Utils.sleep(60000l * 60 * 2);
            }else{
                Utils.sleep(60000l * 10);
            }
            current = new DateTime();
        }
    }

    private void clearUp(){
        long clearTimestamp = (System.currentTimeMillis() / (60*60*24 * 1000)) * (60*60*24 * 1000) - cleanDays* 60*60*24 * 1000;
        String  condition = "pickTimestamp <'"+ clearTimestamp +"'";
        realTimeInfoTempDao.remove(condition);
    }
}
